﻿function BtnDelOnClick(pStrUrl, pListName, pRowName) {
    $('#' + pListName).val(JGetAllCheckboxID(pRowName));
    if ($('#' + pListName).val() == "")
    {
        alert("Chưa có dòng nào được chọn");
        return false;
    }
    else
    {
        $('form').attr('action', pStrUrl); //'/Listtype/Delete'
        $('form').submit(this.submit);
    }
}

function btnAddOnclick(pStrUrl, pItemName) {
    $("#" + pItemName).val("");    
    $('form').attr('action', pStrUrl); //'/Listtype/Detail'
    $('form').submit(this.submit);
}

function btnFilterOnclick(pStrUrl, pStrPageName) {
    $("#" + pStrPageName).val("1");
    $('form').attr('action', pStrUrl); //'/Listtype/Detail'
    $('form').submit(this.submit);
}

function btnFirstPageOnclick(pStrPageName) {
    $("#" + pStrPageName).val("1");
    $('form').submit(this.submit);
}

function btnPrePageOnclick(pStrPageName) {
    var vCurrentPage = StringToInt($("#" + pStrPageName).val());
    if (vCurrentPage > 1 ) {
        $("#" + pStrPageName).val(vCurrentPage - 1);
        $('form').submit(this.submit);
    }
}

function btnNextPageOnclick(pStrPageName) {
    var vTotalPage = StringToInt($("#" + pStrPageName + " option:last").val());
    var vCurrentPage = StringToInt($("#" + pStrPageName).val());
    if (vCurrentPage < vTotalPage) {
        $("#" + pStrPageName).val(vCurrentPage + 1);
        $('form').submit(this.submit);
    }
}

function btnLastPageOnclick(pStrPageName) {
    var vTotalPage = $("#" + pStrPageName + " option:last").val();
    $("#" + pStrPageName).val(vTotalPage);
    $('form').submit(this.submit);
}

function btnPageOnclick() {
    $('form').submit(this.submit);
}

function btnNumerRecordPerPageOnclick() {
    $('form').submit(this.submit);
}

//Chuyen tu xau sang so Float
function StringToFloat(pString) {
    //Convert sang so he so 10
    var vFloat = parseFloat(pString);
    if (isNaN(vFloat)) {
        return 0;
    }
    else {
        return vFloat;
    }
}
//Chuyen tu xau sang so Int
function StringToInt(pString) {
    //Convert sang so he so 10
    var vInt = parseInt(pString, 10);
    if (isNaN(vInt)) {
        return 0;
    }
    else {
        return vInt;
    }
}

function StringToDouble(pString) {
    //Convert sang so he so 10
    var vFloat = parseFloat(pString);
    if (isNaN(vFloat)) {
        return 0;
    }
    else {
        return vFloat;
    }
}

function JResetAllRowSelected(name) {
    jQuery("input[name=" + name + "]").each(function () {
        $("#LO" + this.value).css("background-color", "#F3F6FA");
    });
}


function JShowRowSelected(name) {
    jQuery("input[name=" + name + "]").each(function () {
        if ($('#' + this.id).is(':checked')) {
            //$('#LO'+this.value).attr("class", "midle_row");   
            $("#LO" + this.value).css("background-color", "#FFFFCC");
        }
        else {
            //$('#LO'+this.value).attr("class", "odd_row"); 
            $("#LO" + this.value).css("background-color", "#F3F6FA");
        }
    });
}

function JCheckAll(id, name) {
    $('input:checkbox[name=' + name + ']').attr('checked', $('#' + id).is(':checked'));
    JShowRowSelected(name);
}



function JSelectBuySellOnclick(type) {
    var vBoolean = true;
    if (type == "BUY") {
        jQuery("input[name=chkOrder]").each(function () {

            if ($('#hdnOrder' + this.value).val().indexOf("B") >= 0) {
                $('#' + this.id).attr('checked', true);
            }
            else {
                $('#' + this.id).attr('checked', false);
                vBoolean = false;
            }
        });
    }
    else {
        jQuery("input[name=chkOrder]").each(function () {
            if ($('#hdnOrder' + this.value).val().indexOf("S") >= 0) {
                $('#' + this.id).attr('checked', true);
            }
            else {
                $('#' + this.id).attr('checked', false);
                vBoolean = false;
            }
        });
    }
    if (vBoolean)
        $('#chkAllOrder').attr('checked', true);
    else
        $('#chkAllOrder').attr('checked', false);
    JShowRowSelected("chkOrder");
}

function JItemOnclick(id, itemId, itemName) {
    if (!$('#' + itemId).is(':checked')) {
        $('#' + id).attr('checked', false);
        //$('#LO'+$('#'+itemId).val()).attr("class", "odd_row");   
        $('#LO' + $('#' + itemId).val()).css("background-color", "#F3F6FA");
    }
    else {
        var vBoolean = true;
        jQuery("input[name=" + itemName + "]").each(function () {
            if (vBoolean && !$('#' + this.id).is(':checked')) {
                vBoolean = false;
            }
            if ($('#' + this.id).is(':checked')) {
                //$('#LO'+this.value).attr("class","midle_row");      
                $("#LO" + this.value).css("background-color", "#FFFFCC");
            }
            else {
                //$('#LO'+this.value).attr("class","odd_row"); 
                $("#LO" + this.value).css("background-color", "#F3F6FA");
            }
        });
        if (vBoolean) $('#' + id).attr('checked', true);
    }


}

function JGetAllCheckboxID(name) {
    var vList = "";
    jQuery("input[type=checkbox][name=" + name + "]").each(function () {
        if ($('#' + this.id).is(':checked')) {
            vList = ListAppend(vList, jQuery(this).val(), ",");
        }
    });
    return vList;
}

// Lay phan tu dau tien cua danh sach
function ListGetFirst(the_list, the_separator) {
    if (the_list == "") return "";
    arr_value = the_list.split(the_separator);
    return arr_value[0];
}

// Kiem tra phan tu the_element co trong danh sach the_list hay khong
function ListHaveElement(the_list, the_element, the_separator) {
    try {
        if (the_list == "") return -1;
        if (the_list == the_element) return 1;
        if (the_list.indexOf(the_separator) == -1) return -1;
        arr_value = the_list.split(the_separator);
        for (var i = 0; i < arr_value.length; i++) {
            if (arr_value[i] == the_element) {
                return i;
            }
        }
    } catch (e) { ; }
    return -1;
}
//Dem so phan tu trong danh sach
function ListCountElement(the_list, the_separator) {
    if (the_list == "") return -1;
    arr_value = the_list.split(the_separator);
    if (arr_value.length > 0) {
        return arr_value.length;
    }
    return -1;
}
// add a value to a list
function ListAppend(the_list, the_value, the_separator) {
    var list = the_list;
    the_value = the_value + ""; //Chuyen the_value sang kieu xau
    if (list == "") list = the_value;
    else if (the_value != "") list = list + the_separator + the_value;
    return list;
}
